<?php

/**
 * Fech results from cms DB
 */
class cms extends CI_Model {

    public function all_posts($parent_ID = NULL, $post_type = 'page', $not_ID=NULL) {

        $binding = array($post_type);

        $sql = "SELECT * FROM posts_pic_seo WHERE post_type = ? ";

        if (isset($parent_ID)):
            $sql.=" AND parent_ID = ? ";
            $binding[] = $parent_ID;

        endif;
        
        if (isset($not_ID)):
            $sql.=" AND ID NOT IN (?)";
            $binding[] = $not_ID;

        endif;


        $res = $this->db->query($sql, $binding);

        return $res->result_array();
    }

    public function get_post($ID) {

        $sql = "SELECT * FROM posts_pic_seo WHERE ID = ? ";

        $res = $this->db->query($sql, array($ID));

        return $res->row_array();
    }

    public function get_pictures($post_ID) {

        $sql = "SELECT * FROM pic_gallery WHERE post_ID = ? ";

        $res = $this->db->query($sql, array($post_ID));

        return $res->result_array();
    }

    public function get_files($post_ID) {

        $sql = "SELECT * FROM files_folders WHERE post_ID = ? ";

        $res = $this->db->query($sql, array($post_ID));

        return $res->result_array();
    }

    public function find_top($ID) {
        
        $sql = "SELECT ID FROM posts
        WHERE parent_ID = ?
        ORDER BY ID asc
        LIMIT 1";

        $res = $this->db->query($sql, array($ID))->row_array();
        
        return count($res) > 0 ? $res['ID'] : 0 ;
    }

}

?>
